package util
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.core.UIComponent;

	public class SmallSpinner extends UIComponent
	{	
	
	    private var _childs:Array;
	    private var act_idx:int;
	    private var timer:Timer;
	    private var radius1:Number;
	    private var radius2:Number;
	    private var delay:Number;
	
		public function SmallSpinner(radius1:Number=3,radius2:Number=10,delay:Number=100)
		{
			super();
			this.radius1 = radius1;
			this.radius2 = radius2;
			this.delay = delay;
            init();
            startTick();
		}
		
		public function tick(e:Event=null):void {
			act_idx = (++act_idx)%9;
			for (var i:int=0;i<9;i++) {
			  _childs[(i+act_idx)%9].alpha = 1.0/9.0*i;
			}
			//trace(tick,new Date().milliseconds)
		}
		
		
		public function startTick():void {
		    if (!timer) {
		      timer = new Timer(delay,0);
		    }
		    
		    if (timer.running) {
		    	return;
		    }
		    
		    timer.addEventListener(TimerEvent.TIMER,tick);		      
		    timer.start();   
		}
		
		public function stopTick():void {
		  if (timer && timer.running) {
		  	timer.stop();
		  	timer.removeEventListener(TimerEvent.TIMER,tick);
		  }	
			
		}
		private function setupChild(idx:int):void {
           var s:Shape = new Shape();
           var g:Graphics = s.graphics;
           
           g.beginFill(0x0);
           g.drawCircle(0,radius2,radius1);
           g.endFill();
           		
           		
           		
           addChild(s);
           s.rotation = idx*45;	
           _childs[idx] = s;	
           
        }
		
		
		private function init():void {
            //graphics.beginFill(0xf0f0f0);
            //graphics.drawRect(-60,-40,120,80);
            //graphics.endFill();
	        _childs = new Array(9); //Vector.<Shape>(9);       
	        for (var i:int=0;i<9;i++) setupChild(i);
	    }
		
	}
}